<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>xiaxuchen的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Android消息机制一、 Handler 简介 进行线程的转换，比如最常用的通过Handler切换到主线程更新UI，但他的作用不仅仅是更新UI. android为了保持高效且简单，使用单线程模型来处理UI,若在其他线程更新UI则会抛异常，如下ViewRootImpl中的代码： 1234if (mThread !&#x3D; Thread.currentThread()) &amp;#123;      throw">
<meta property="og:type" content="article">
<meta property="og:title" content="xiaxuchen的博客">
<meta property="og:url" content="http://yoursite.com/2020/08/07/%E5%90%8E%E7%AB%AF/Android%20Handler/index.html">
<meta property="og:site_name" content="xiaxuchen的博客">
<meta property="og:description" content="Android消息机制一、 Handler 简介 进行线程的转换，比如最常用的通过Handler切换到主线程更新UI，但他的作用不仅仅是更新UI. android为了保持高效且简单，使用单线程模型来处理UI,若在其他线程更新UI则会抛异常，如下ViewRootImpl中的代码： 1234if (mThread !&#x3D; Thread.currentThread()) &amp;#123;      throw">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-07T15:08:02.028Z">
<meta property="article:modified_time" content="2020-07-19T10:36:54.023Z">
<meta property="article:author" content="xiaxuchen">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="./atom.xml" title="xiaxuchen的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="./css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="./" id="logo">xiaxuchen的博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="./">Home</a>
        
          <a class="main-nav-link" href="./archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="./atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-后端/Android Handler" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="./2020/08/07/%E5%90%8E%E7%AB%AF/Android%20Handler/" class="article-date">
  <time datetime="2020-08-07T15:08:02.028Z" itemprop="datePublished">2020-08-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="Android消息机制"><a href="#Android消息机制" class="headerlink" title="Android消息机制"></a>Android消息机制</h3><h3 id="一、-Handler"><a href="#一、-Handler" class="headerlink" title="一、 Handler"></a>一、 Handler</h3><ul>
<li><p>简介</p>
<p>进行线程的转换，比如最常用的通过Handler切换到主线程更新UI，但他的作用不仅仅是更新UI.</p>
<p>android为了保持高效且简单，使用单线程模型来处理UI,若在其他线程更新UI则会抛异常，如下ViewRootImpl中的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (mThread != Thread.currentThread()) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> CalledFromWrongThreadException(</span><br><span class="line">              <span class="string">&quot;Only the original thread that created a view hierarchy can touch its views.&quot;</span>);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建</p>
<p>在某个线程创建Handler就会去获取当前线程的Looper，若没有则会抛异常，所以需要创建Looper</p>
</li>
<li><p>使用</p>
<p>一般我们会通过post方法将一个runnable投递到Looper中执行，或者调用send方法发送消息，最终仍是在looper中执行，而post原理也是通过send进行的实现</p>
<p>send的原理</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">graph TB</span><br><span class="line">currentThread --handler.send--&gt; 消息将会入队Handler中的MessageQueue --looper获取消息--&gt; looper线程消费消息</span><br></pre></td></tr></table></figure>
</li>
<li><p>MessageQueue</p>
<p>消息队列，使用单向链表实现，用于存取消息</p>
</li>
<li><p>Looper</p>
<ul>
<li><p>使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Looper.prepare(); &#x2F;&#x2F; 为当前线程创建一个Looper</span><br><span class="line">Handler handler &#x3D; new Handler(); &#x2F;&#x2F; 创建handler自动获取looper</span><br><span class="line">Looper.loop(); &#x2F;&#x2F; 开启looper循环</span><br><span class="line">Looper.quit(); &#x2F;&#x2F; 立即停止looper的执行</span><br><span class="line">Looper.quitSafely(); &#x2F;&#x2F; 执行完当前所有的消息后再停止</span><br></pre></td></tr></table></figure>

<p>注意,当looper退出时，使用send将返回false</p>
</li>
</ul>
</li>
<li><p>构造方法</p>
<p>Handler(CallBack)</p>
<p>可以传递一个CallBack对象给Handler，当Message中没有callback时调用，若CallBack.handleMessage返回false则会回调Handler的handleMessage方法</p>
<p>Handler(Looper)</p>
<p>可以给Handler指定Looper</p>
</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/08/07/%E5%90%8E%E7%AB%AF/Android%20Handler/" data-id="ckdkdagut000gvoutdwxc7ivj" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="./2020/08/07/%E5%90%8E%E7%AB%AF/java%E6%95%B0%E6%8D%AE%E5%BA%93%E6%A0%87%E5%87%86/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="./2020/08/07/%E5%B7%A5%E5%85%B7/%E5%90%88%E5%B9%B6%E5%88%86%E6%9E%9D%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="./archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E9%93%BE%E8%A1%A8/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%95%B0%E7%BB%84/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/SpringBoot%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 xiaxuchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="./" class="mobile-nav-link">Home</a>
  
    <a href="./archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="./fancybox/jquery.fancybox.css">

  
<script src="./fancybox/jquery.fancybox.pack.js"></script>




<script src="./js/script.js"></script>




  </div>
</body>
</html>